//
//  HMessageTool.m
//  linktuIOS
//
//  Created by hejunsong on 2016/11/19.
//  Copyright © 2016年 tianxie. All rights reserved.
//

#import "HMessageTool.h"
#import "YYImage.h"
#import "YYTextLayout.h"
#import "YYText.h"
#import "HLiveMesssage.h"
#import "SDImageCache.h"
#define  namesize  16
#define  contentsize 16
#define  nameColor       mainTintColor
#define  lightColor      RGB(218, 255, 118)
#define  genrelColor     [UIColor whiteColor]
#define  imagefont       [UIFont systemFontOfSize:16]
#define  fontStr     @"Helvetica-Bold"
static HMessageTool *helper = nil;

@implementation HMessageTool
+ (instancetype)messgeTool
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        helper = [[HMessageTool alloc] init];
        helper.giftdict = [NSMutableDictionary dictionary];
        
    });
    return helper;
}

-(void)addMessageAndShow:(LTMessage *)message withSuccesss:(void (^)(HLiveMesssage *liveMessage,BOOL success))result
{
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
         LTUser *user = message.since;
         UIColor *contentColor = nil;
         // 创建属性字符串
         NSMutableAttributedString *textname = nil;
         NSMutableAttributedString *content  =  nil;
         NSMutableAttributedString *imagecontent = nil;
        
         YYTextHighlight *highlight = [YYTextHighlight new];
         [highlight setColor:[UIColor whiteColor]];

         textname = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@:",user.name]];
        
         textname.yy_font = [UIFont fontWithName:fontStr size:namesize];
         textname.yy_color = nameColor;
         YYTextShadow *shadow = [YYTextShadow new];
         shadow.color = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
         shadow.offset = CGSizeMake(0.5, 0.5);
         textname.yy_textShadow = shadow;
         [textname yy_setTextHighlight:highlight range:NSMakeRange(0, user.name.length)];
         NSString *tempStr = @"";
        
         switch (message.messageType) {
            case LTMessageTypeText:
            {
                LTMessageTextBody *body =(LTMessageTextBody *)message.body;
                tempStr = body.text;
                contentColor = genrelColor;
                
            }break;
            case LTMessageTypeLightUp:
            {
                LTMessageLightUpBody *body = (LTMessageLightUpBody *)message.body;
                if(body.isFirst)
                {
                    tempStr = NSLTLocalizedString(@"点亮了", nil);
                    // 嵌入 UIImage
                    UIImage *image = [UIImage imageNamed:@"icon_Light_light"];
                    imagecontent = [NSMutableAttributedString yy_attachmentStringWithContent:image contentMode:UIViewContentModeScaleToFill attachmentSize:CGSizeMake(16, 16) alignToFont:imagefont alignment:YYTextVerticalAlignmentCenter];
                    contentColor = lightColor;
                }else
                {
                    result(nil,NO);
                    return;
                }
            }break;
            case LTMessageTypeGift:
            {
                LTMessageGiftBody *body = (LTMessageGiftBody *)message.body;
                contentColor = genrelColor;
                tempStr = [NSString stringWithFormat:@"送了一个%@",body.giftName];
            }break;
            case LTMessageTypeBarrage:
            {
                LTMessageTextBody *body =(LTMessageTextBody *)message.body;
                tempStr = body.text;
                contentColor = genrelColor;
            }break;
             case LTMessageTypeRoomNormal: //直播间普通文本消息
             {
                 
             }break;
             case LTMessageTypeRoomJoin:  //进入直播间消息
             {
                 textname = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@""]];
                 tempStr = [NSString stringWithFormat:@"%@加入了直播间",user.name];
                 contentColor = genrelColor;
             }break;
             case LTMessageTypeRoomSetManager:  //设置为管理员
             {
                 LTMessageRoomOperationBody *body = (LTMessageRoomOperationBody *)message.body;
                 textname = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@""]];
                 tempStr = [NSString stringWithFormat:@"%@被主播设为了管理员",body.operationUser.name];
                 contentColor = genrelColor;
             }break;
             case LTMessageTypeRoomCancelManager:  //取消管理员
             {
                 LTMessageRoomOperationBody *body = (LTMessageRoomOperationBody *)message.body;
                 textname = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@""]];
                 tempStr = [NSString stringWithFormat:@"主播取消了%@管理员权限",body.operationUser.name];
                 contentColor = genrelColor;
             }break;
             case LTMessageTypeRoomMute:  //禁言用户
             {
                 LTMessageRoomOperationBody *body = (LTMessageRoomOperationBody *)message.body;
                 textname = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@""]];
                 tempStr = [NSString stringWithFormat:@"%@被管理员禁言",body.operationUser.name];
                 contentColor = genrelColor;
             }break;
             case LTMessageTypeRoomCancelMute:  //取消禁言
             {
                 LTMessageRoomOperationBody *body = (LTMessageRoomOperationBody *)message.body;
                 textname = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@""]];
                 tempStr = [NSString stringWithFormat:@"%@被管理员取消了禁言",body.operationUser.name];
                 contentColor = genrelColor;
             }break;
             case LTMessageTypeRoomKickout:  //踢出用户
             {
                 LTMessageRoomOperationBody *body = (LTMessageRoomOperationBody *)message.body;
                 textname = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@""]];
                 tempStr = [NSString stringWithFormat:@"%@被管理员踢出了房间",body.operationUser.name];
                 contentColor = genrelColor;
                 
             }break;
             case LTMessageTypeRoomUpdateTitle:  //更新title
             {
                 LTMessageUpdateTitleBody *body =(LTMessageUpdateTitleBody *)message.body;
                 tempStr = body.title;
                 contentColor = genrelColor;
                 textname = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@""]];
                 tempStr = [NSString stringWithFormat:@"主播更新了标题:%@",tempStr];
                 contentColor = genrelColor;
             }break;
             case LTMessageTypeRoomFocusHost:
             {
                 textname = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@""]];
                 tempStr = [NSString stringWithFormat:@"%@关注了主播",user.name];
                 contentColor = genrelColor;
             }break;
             default:
             {
                result(nil,NO);
                return;
             } break;
         }
        content =  [[NSMutableAttributedString alloc] initWithString:tempStr];
        
        content.yy_font = [UIFont fontWithName:fontStr size:contentsize];
        content.yy_color =  contentColor;
        YYTextShadow *contentshadow = [YYTextShadow new];
        
        contentshadow.color =  [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        contentshadow.offset = CGSizeMake(0.5, 0.5);
        content.yy_textShadow = contentshadow;

        [textname appendAttributedString:content];
        if(imagecontent != nil)
        {
            [textname appendAttributedString:imagecontent];
        }
        textname.yy_lineSpacing = -2;
        
        YYTextContainer *container = [YYTextContainer new];
        container.size = CGSizeMake([UIScreen mainScreen].bounds.size.width  - 60, CGFLOAT_MAX);
        container.maximumNumberOfRows = 0;
        
        // 生成排版结果
        YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:textname];
        
        HLiveMesssage *livemessage = [[HLiveMesssage alloc] init];
        livemessage.ccntentString = textname;
        livemessage.message = message;
        livemessage.layout = layout;
        dispatch_async(dispatch_get_main_queue(), ^{
            result(livemessage,YES);
        });
    });

    
}
////设置礼物等级标识
- (NSMutableAttributedString *)setupGiftLevelBtnWithLevel:(NSString *)level {

   
    if(level != nil)
    {
        UIButton *giftLevelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        giftLevelBtn.titleLabel.font = [UIFont systemFontOfSize:9];
        [giftLevelBtn setTitle:level forState:UIControlStateNormal];
        giftLevelBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        
        NSInteger levelNumber = [level integerValue];
        
        if (levelNumber <= 0) {
            [giftLevelBtn setBackgroundImage:[UIImage imageNamed:@"1~20"] forState:UIControlStateNormal];
            [giftLevelBtn setTitle:@"1" forState:UIControlStateNormal];

        }else if (levelNumber < 21) {
            [giftLevelBtn setBackgroundImage:[UIImage imageNamed:@"1~20"] forState:UIControlStateNormal];
        }else if (levelNumber < 41) {
            [giftLevelBtn setBackgroundImage:[UIImage imageNamed:@"21~40"] forState:UIControlStateNormal];
        }else if (levelNumber < 61) {
            [giftLevelBtn setBackgroundImage:[UIImage imageNamed:@"41~60"] forState:UIControlStateNormal];
        }else if (levelNumber < 81) {
            [giftLevelBtn setBackgroundImage:[UIImage imageNamed:@"61~80"] forState:UIControlStateNormal];
        }else {
            [giftLevelBtn setBackgroundImage:[UIImage imageNamed:@"81~100"] forState:UIControlStateNormal];
        }
        NSMutableAttributedString *giftContent = [NSMutableAttributedString yy_attachmentStringWithContent:giftLevelBtn contentMode:UIViewContentModeScaleToFill attachmentSize:CGSizeMake(RelativeValue(40), 16) alignToFont:imagefont alignment:YYTextVerticalAlignmentCenter];
        return giftContent;

    }else
    {
        return [[NSMutableAttributedString alloc] init];
    }
  }


@end
